/*******************************************************************************
 * Copyright 2011 Danny Kunz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.omnaest.maven.plugin.technology_timeline;

import java.io.File;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Helper class for analyzing the zip files
 * 
 * @author Omnaest
 */
public class ZipFileScanner
{
  /**
   * @param file
   * @return
   */
  public static Calendar resolveLastFileCreationDateOfTopLevelFilesWithinZipFile( File file )
  {
    //
    Calendar retval = null;
    
    //
    try
    {
      ZipFile zipFile = new ZipFile( file );
      try
      {
        //
        for ( Enumeration<? extends ZipEntry> zipFileEntryEnumeration = zipFile.entries(); zipFileEntryEnumeration.hasMoreElements(); )
        {
          //
          ZipEntry zipEntry = zipFileEntryEnumeration.nextElement();
          long timeInMilliseconds = zipEntry.getTime();
          
          //
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis( timeInMilliseconds );
          
          //
          if ( retval == null || calendar.after( retval ) )
          {
            retval = calendar;
          }
        }
      }
      catch ( Exception e )
      {
        
      }
      finally
      {
        zipFile.close();
      }
    }
    catch ( Exception e )
    {
      
    }
    
    //
    return retval;
  }
}
